﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace CommonTools
{
    public partial class TFSButton : UserControl
    {
        public TFSButton()
        {
            InitializeComponent();
            TFHelper = new TFHelper();

            AddCommandMenuItem(TFCommands.Get, 0);
            AddCommandMenuItem(TFCommands.CheckOut, 1);
            AddCommandMenuItem(TFCommands.CheckIn, 2);
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public TFHelper TFHelper { get; set; }

        private void btnTFS_Click(object sender, EventArgs e)
        {
            contextMenuStripTFS.Show(btnTFS, 0, btnTFS.Height);
        }

        private void contextMenuStripTFS_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
                e.Cancel = true;
        }

        public void AddCommandMenuItem(TFCommands command, int index = 0)
        {
            var commandMenuItem = new ToolStripMenuItem();

            commandMenuItem.Tag = command;
            commandMenuItem.Text = TFHelper.ParseCamelCase(command.ToString());
            commandMenuItem.CheckOnClick = true;
            commandMenuItem.CheckedChanged += commandMenuItem_CheckedChanged;

            contextMenuStripTFS.Items.Insert(index, commandMenuItem);
        }

        public void SetSupportedCommands()
        {
            foreach (ToolStripMenuItem item in contextMenuStripTFS.Items)
                if (item.Tag is TFCommands)
                {
                    item.CheckedChanged -= commandMenuItem_CheckedChanged;
                    item.Checked = TFHelper.SupportedCommands.HasFlag((TFCommands)item.Tag);
                    item.CheckedChanged += commandMenuItem_CheckedChanged;
                }
        }

        private void commandMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            var mi = (ToolStripMenuItem)sender;

            if (mi.Tag is TFCommands)
            {
                var command = (TFCommands)mi.Tag;

                if (mi.Checked)
                    TFHelper.SupportedCommands = TFHelper.SupportedCommands | command;
                else
                    TFHelper.SupportedCommands = TFHelper.SupportedCommands ^ command;
            }
        }

        private void tfPathMenuItem_Click(object sender, EventArgs e)
        {
            openTFLocationDialog.InitialDirectory = Path.GetDirectoryName(TFHelper.TFPath);
            openTFLocationDialog.FileName = TFHelper.TFPath;
            contextMenuStripTFS.Close(ToolStripDropDownCloseReason.CloseCalled);

            if (openTFLocationDialog.ShowDialog() == DialogResult.OK)
            {
                TFHelper.TFPath = openTFLocationDialog.FileName;
            }
        }
    }
}
